package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.reggie.bean.AddressBook;
import com.zrrd.reggie.common.BaseContext;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List> list() {
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("is_default");
        return R.success(addressBookService.list(wrapper));
    }

    //添加地址
    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook) {
        //
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        List<AddressBook> AddressBooks = addressBookService.list(wrapper);
        if (AddressBooks == null || AddressBooks.size() <= 0) {
            addressBook.setIsDefault(1);
        }
        addressBook.setUserId(BaseContext.getCurrentId());
        return addressBookService.save(addressBook) ? R.success("添加成功") : R.error("添加失败");
    }

    //根据ID展示地址信息
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBook(@PathVariable("id") Long id) {
        return R.success(addressBookService.getById(id));
    }

    //修改地址
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        return addressBookService.updateById(addressBook) ? R.success("修改成功") : R.error("修改失败");
    }

    //删除地址
    @DeleteMapping
    public R<String> deleteAddressBook(Long[] ids) {
        return addressBookService.removeByIds(Arrays.asList(ids)) ? R.success("删除成功") : R.error("删除失败");
    }

    //设为默认地址
    @PutMapping("/default")
    public R<String> updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        List<AddressBook> addressBookList = addressBookService.list();
        for (AddressBook address : addressBookList) {
            address.setIsDefault(0);
        }
        addressBookService.updateBatchById(addressBookList);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("已设为默认地址");
    }

    //获取默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressBook() {
        System.out.println("测试提交分支");
        System.out.println("再次测试提交分支");
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.eq("is_default", 1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("用户没有默认地址");
        }
    }

}
